<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="../public/public.jsp"%>
<%@ include file="../public/buttons.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户管理</title>

<script type="text/javascript">
   
   	$(function () {
	    // 查询
		$('#010101').click(function() {
			$('#tt').datagrid({
		        animate: true,        //是否动画展开折叠
		        checkbox: false, //全选复选框
		        checkOnSelect: true, //选中复选框的同时选中行
		        selectOnCheck: true, //选中行的同时选中复选框
		        singleSelect: true, //是否单选
		        collapsible: true,    //是否可折叠
		        height: 'auto',  //自动高度
		        iconCls: 'icon-save',  //样式图标
		        idField: 'user_id',   //主索引，唯一标识字段
		        //loadMsg: '数据载入中，请稍候……',
		        maximizable: true,
		        nowrap: true,  //截断内文
		        pagination: true, //分页
		        pageList: [10,20,30,40,50],
		        rownumbers: true,   //是否显示列数,
			    url: '${ctx}/sysUser/userList.do',
			    frozenColumns: [[
					{field:'user_id',title:'唯一id',hidden:true},       
					{field:'ck',checkbox:true},
		   	        {field:'login_name',title:'登录账号',width:200}	   	        
		   	    ]],
			    columns: [[
					{field:'real_name',title:'真实姓名',width:200},
					{field:'email',title:'电子邮箱',width:200},
				    {field:'state',title:'用户状态',width:120,
					    formatter:function (value) {
					    	if(value ==0){
					    		return "<font color=red>注销</font>";
					    	}else if(value ==1){
					    		return "正常";
					    	}
					    }
				    },  
				    {field:'create_time',title:'创建时间',width:200,
				    	formatter:function (value) {
					        return value == null ? '' : (new Date(value).format('yyyy-MM-dd hh:mm:ss'));
					    }
				    }
			    ]],
			    onBeforeLoad: function (param) {
			    	param.login_name = $('#login_name').searchbox('getValue');
		        },
		        onLoadSuccess: function (result){
		        	if(!result.success){
			        	$.messager.alert('操作提示', result.opMsg, 'warning');
		        	}	
		        },
		        onDblClickRow:function(rowIndex, rowData)
				{
		        	$('#w_viewUser').window('open');
		        	$('#f_viewUser').form('load',rowData);
		        	var user_stats = rowData.state;
		        	if(user_stats==0)
		        	{
		        		$('#viewUser_stats').val('注销');
		        	}else if(user_stats ==1){
		        		$('#viewUser_stats').val('正常');
			    	}
		        	//ajax获得对应的绑定角色
		        	var params = {};
		    		params.user_id = rowData.user_id;
		    		$.post('${ctx}/sysUser/bindedRoles.do', params, function(result) {
		    			if (result.success) {
		    				$('#viewUser_bindedRoles').val(result.data.roleNames);
		    			}
		    			if(result.success == false){
		    				$.messager.alert('操作提示', result.opMsg, 'warning');
		    			}
		    		});
				}
			});
			reset_pagenum($('#tt'));//重置当前页为1
		});
		$('#010101').click();//加载数据
		
		$('#addUser_btnOk').click(function () { 
		    save_addUser();
		});
		$('#addUser_btnCancel').click(function () {
			clean_add_form();
		    $('#w_addUser').window('close');
	    }); 
		$('#editUser_btnOk').click(function () { 
		    save_editUser();
		});
		$('#editUser_btnCancel').click(function () {
			clean_edit_form();
		    $('#w_editUser').window('close');
	    }); 
    });
   
	function addUser() {
		$('#w_addUser').window('open');
		loadTree();
	}
	
	function save_addUser()
	{
		if (!$('#add_login_name').validatebox('isValid')
				|| !$('#add_real_name').validatebox('isValid')
				|| !$('#add_email').validatebox('isValid')
				|| !$('#userForRoleSel').validatebox('isValid')) {
			return false;
		}
		var params = {};
		params.login_name = $('#add_login_name').val();
		params.real_name = $('#add_real_name').val();
		params.email = $('#add_email').val();
		params.remarks = $('#add_remarks').val();
		params.selectRole = $('#selectRole').val();
		$.post('${ctx}/sysUser/addUser.do', params, function(result) {
			if (result.success) {
				clean_add_form();
				$('#w_addUser').window('close');
				$.messager.alert('操作提示', result.opMsg, 'info');
				reload_dg($('#tt'));//刷新列表
			}
			if(result.success == false){
				$.messager.alert('操作提示', result.opMsg, 'warning');
			}
		});
	}
	
	function deleteUser(){
	    var row = $('#tt').datagrid('getSelected');
	    if (row){
	    	if(row.state==0){
	    		$.messager.alert('操作提示','该用户已删除！','warning');
	    		return;
	    	}else{
	    		$.messager.confirm('操作提示','确定删除？',function(f) {
	    		    if (f) {
	    		    	$.ajax({
		  					  type : 'post',
		  					  url : '${ctx}/sysUser/deleteUser.do?user_id='+row.user_id,
		  					  success:function(result){
		  						  if(result.success){
		  							  $.messager.alert('操作提示', result.opMsg,'info');
			  						  reload_dg($('#tt'));//刷新列表 
		  						  }
		  						  if(result.success==false){
		  							  $.messager.alert('操作提示', result.opMsg,'warning');
		  						  }
		  					  }
		  				  });
	    		    }
	    		    else {
	    				return;
	    		    }
	    		});
	    	}
	    }else{
	    	$.messager.alert('操作提示','请选择需要删除的用户！','warning');
	    	return;
	    }
   }
	
	function resetPwd(){
	    var row = $('#tt').datagrid('getSelected');
	    if (row){
	    		$.messager.confirm('操作提示','确定重置密码？',function(f) {
	    		    if (f) {
	    		    	$.ajax({
		  					  type : 'post',
		  					  url : '${ctx}/sysUser/resetPwd.do?login_name='+row.login_name,
		  					  success:function(result){
		  						  if(result.success){
		  							  $.messager.alert('操作提示', result.opMsg,'info');
			  						  reload_dg($('#tt'));//刷新列表 
		  						  }
		  						  if(result.success==false){
		  							  $.messager.alert('操作提示', result.opMsg,'warning');
		  						  }
		  					  }
		  				  });
	    		    }
	    		    else {
	    				return;
	    		    }
	    		});
	    }else{
	    	$.messager.alert('操作提示','请选择需要重置密码的用户！','warning');
	    	return;
	    }
   }
	
   function editUser(){
	    var row = $('#tt').datagrid('getSelected');
	    if (row){
	    	$('#w_editUser').window('open');
	    	$('#f_editUser').form('load',row);
	    	//ajax获得对应的绑定角色
        	var params = {};
    		params.user_id = row.user_id;
    		$.post('${ctx}/sysUser/bindedRoles.do', params, function(result) {
    			if (result.success) {
    				$('#userForRoleSel_edit').val(result.data.roleNames);
    				$('#selectRole_edit').val(result.data.roleIds);
    				loadTree_edit();
    			}
    			if(result.success == false){
    				$.messager.alert('操作提示', result.opMsg, 'warning');
    			}
    		});
	    }else{
	    	$.messager.alert('操作提示','请选择需要修改的用户！','warning');
	    	return;
	    }
   }
   
   function save_editUser()
	{
		if (!$('#edit_real_name').validatebox('isValid')
				|| !$('#edit_email').validatebox('isValid')
				|| !$('#userForRoleSel_edit').validatebox('isValid')) {
			return false;
		}
		var params = {};
		params.user_id = $('#tt').datagrid('getSelected').user_id;
		params.login_name = $('#edit_login_name').val();
		params.real_name = $('#edit_real_name').val();
		params.email = $('#edit_email').val();
		params.remarks = $('#edit_remarks').val();
		params.selectRole = $('#selectRole_edit').val();
		$.post('${ctx}/sysUser/editUser.do', params, function(result) {
			if (result.success) {
				clean_edit_form();
				$('#w_editUser').window('close');
				$.messager.alert('操作提示', result.opMsg, 'info');
				reload_dg($('#tt'));//刷新列表
			}
			if(result.success == false){
				$.messager.alert('操作提示', result.opMsg, 'warning');
			}
		});
	}
	
   function clean_add_form()
   {
	   $('#add_login_name').val('');
	   $('#add_real_name').val('');
	   $('#add_email').val('');
	   $('#userForRoleSel').val('');
	   $('#selectRole').val('');
	   $('#add_remarks').val('');
   }
   
   function clean_edit_form()
   {
	   $('#edit_login_name').val('');
	   $('#edit_real_name').val('');
	   $('#edit_email').val('');
	   $('#userForRoleSel_edit').val('');
	   $('#selectRole_edit').val('');
	   $('#edit_remarks').val('');
   }
   
</script>
<script type="text/javascript">
	  	var setting = {
		    check: {
				enable: true,
				chkStyle: "checkbox",
		        chkboxType: { "Y":"s", "N":"s"  }
			},
			callback: {
				onCheck: onCheckForBusiness
			},
			async: {
				enable: true,
				url:"${ctx}/sysUser/ztreeRoles.do",
				dataType: "json",
				dataFilter: null
			},
			data: {
				simpleData: {
					enable: true
				}
			}
		};
		function onCheckForBusiness(e, treeId, treeNode) {
			var zTree = $.fn.zTree.getZTreeObj("treeForRole");
			nodes = zTree.getCheckedNodes(true);
			id = "";
			name = "";
			nodes.sort(function compare(a,b){return a.id-b.id;});
			for (var i=0, l=nodes.length; i<l; i++) {
			    id += nodes[i].id + ",";
				name += nodes[i].name + ",";
			}
			if (name.length > 0 ) name = name.substring(0, name.length-1);
			if (id.length > 0 ) id = id.substring(0, id.length-1);
			var selObj = $("#userForRoleSel");
			selObj.attr("value", name);
			$("#selectRole").val(id);
			$("#userForRoleSel").focus();
		}
		function loadTree(){
        	$.fn.zTree.init($("#treeForRole"), setting);
		}
		function showMenu() {
			var selObj = $("#userForRoleSel");
			var businessOffset = $("#userForRoleSel").offset();
			$("#menuContentForRole").css({left:businessOffset.left -343 + "px", top:businessOffset.top -374 + selObj.outerHeight() + "px"}).slideDown("fast");
			$("body").bind("mousedown", onBodyDownForBusiness);
		}
		function hideMenu() {
		    $("#menuContentForRole").fadeOut("fast");
		}
		function onBodyDownForBusiness(event) {
			if (!(event.target.id == "menuBtnForRole" || event.target.id == "menuContentForRole" || $(event.target).parents("#menuContentForRole").length>0)) {
				hideMenu();
			}
		}
		
</script>
<script type="text/javascript">
		function onCheckForBusiness_edit(e, treeId, treeNode) {
			var zTree = $.fn.zTree.getZTreeObj("treeForRole_edit");
			nodes = zTree.getCheckedNodes(true);
			id = "";
			name = "";
			nodes.sort(function compare(a,b){return a.id-b.id;});
			for (var i=0, l=nodes.length; i<l; i++) {
			    id += nodes[i].id + ",";
				name += nodes[i].name + ",";
			}
			if (name.length > 0 ) name = name.substring(0, name.length-1);
			if (id.length > 0 ) id = id.substring(0, id.length-1);
			var selObj = $("#userForRoleSel_edit");
			selObj.attr("value", name);
			$("#selectRole_edit").val(id);
			$("#userForRoleSel_edit").focus();
		}
		function loadTree_edit(){
			var cur_user_id = $('#tt').datagrid('getSelected').user_id;
			var setting_edit = {
				    check: {
						enable: true,
						chkStyle: "checkbox",
				        chkboxType: { "Y":"s", "N":"s"  }
					},
					callback: {
						onCheck: onCheckForBusiness_edit
					},
					async: {
						enable: true,
						url:"${ctx}/sysUser/ztreeEditRoles.do?user_id=" + cur_user_id ,
						dataType: "json",
						dataFilter: null
					},
					data: {
						simpleData: {
							enable: true
						}
					}
			};
        	$.fn.zTree.init($("#treeForRole_edit"), setting_edit);
		}
		function showMenu_edit() {
			var selObj = $("#userForRoleSel_edit");
			var businessOffset = $("#userForRoleSel_edit").offset();
			$("#menuContentForRole_edit").css({left:businessOffset.left -343 + "px", top:businessOffset.top -374 + selObj.outerHeight() + "px"}).slideDown("fast");
			$("body").bind("mousedown", onBodyDownForBusiness_edit);
		}
		function hideMenu_edit() {
		    $("#menuContentForRole_edit").fadeOut("fast");
		}
		function onBodyDownForBusiness_edit(event) {
			if (!(event.target.id == "menuBtnForRole_edit" || event.target.id == "menuContentForRole_edit" || $(event.target).parents("#menuContentForRole_edit").length>0)) {
				hideMenu_edit();
			}
		}
		
</script>
</head>
<body>
		<div class="easyui-panel" title="用户管理">
		<div id="querybar" style="padding-top:10px;padding-bottom:10px;">
		    <table cellspacing="0" cellpadding="0">
				<tr>
					<td>&nbsp;查询条件：</td>
					<td>
						<input id="login_name" class="easyui-searchbox" data-options="prompt:'登录账号'"></input>
					</td>
					<td>&nbsp;</td>
					<td>
						<a href="javascript:void(0)" class="easyui-linkbutton" id="010101" style="display:none;" iconCls="icon-search" plain="true">查询</a>
					</td>
				</tr>
			</table>
        </div>
        <div id="toolbar">
	        <a href="javascript:void(0)" class="easyui-linkbutton" id="010104" style="display:none;" iconCls="icon-add" plain="true" onclick="addUser()">新增</a>
	        <a href="javascript:void(0)" class="easyui-linkbutton" id="010107" style="display:none;" iconCls="icon-edit" plain="true" onclick="editUser()">修改</a>
	        <a href="javascript:void(0)" class="easyui-linkbutton" id="010106" style="display:none;" iconCls="icon-delete" plain="true" onclick="deleteUser()">删除</a>
	        <a href="javascript:void(0)" class="easyui-linkbutton" id="010109" style="display:none;" iconCls="icon-reset" plain="true" onclick="resetPwd()">重置密码</a>
    	</div>
	    <form id="ff" method="post" style="padding-top:10px;padding-bottom:10px;">
		    <table id="tt" toolbar="#toolbar" ></table>
	    </form>
	    <%-- 新增用户window --%>
	    <div id="w_addUser"  class="easyui-window"  data-options="title:'新增用户',maximizable:false,closed:true,minimizable:false,collapsible:false,width:550,modal:true,resizable:false">
			<div style="margin-top:20px;margin-left:20px;" width="100%" cellspacing="0" cellpadding="0">
			    <div class="fitem">
                	<label>登录账号</label>
		        	<input class="easyui-validatebox" type="text" id="add_login_name" data-options="required:true"></input>
           		</div>
           		<div class="fitem">
                	<label>真实姓名</label>
		        	<input class="easyui-validatebox" type="text" id="add_real_name" data-options="required:true"></input>
           		</div>
           		<div class="fitem">
                	<label>电子邮箱</label>
		        	<input class="easyui-validatebox" type="text" id="add_email" data-options="required:true"></input>
           		</div>
           		<div class="fitem">
                	<label>绑定角色</label>
		        	<input class="easyui-validatebox" type="text" id="userForRoleSel"  name="userForRoleSel" data-options="required:true" readonly="true"></input>
      				<a id="menuBtnForRole" onclick="javascript:showMenu();" href="javascript:void(0);">&nbsp;选择</a>
      				<div id="menuContentForRole" style=" border:1px solid #ccc;width:198px;height:176px;overflow:scroll;scroll-y;display:none;position: absolute; background-color:#fcfcfc;">
						<ul id="treeForRole" class="ztree"	style="margin-top: 0; width: 140px;"></ul>
  					</div>
  					<input type="hidden" id="selectRole" />
           		</div>
           		<div class="fitem">
                	<label>备注</label>
		        	<textarea style="height:70px;width:180px;" id="add_remarks"></textarea>
           		</div>
			</div>
			<div style="padding-left:200px;padding-top:10px;">
				<a href="javascript:void(0);" id="addUser_btnOk" class="easyui-linkbutton" iconCls="icon-ok" >保存</a>
				<a href="javascript:void(0);" id="addUser_btnCancel" class="easyui-linkbutton" iconCls="icon-cancel" >取消</a>
			</div>
	    </div>
	    <%-- 双击查看用户window --%>
	    <div id="w_viewUser"  class="easyui-window"  data-options="title:'查看用户',maximizable:false,closed:true,minimizable:false,collapsible:false,width:450,modal:true,resizable:false">
			<form id="f_viewUser" method="post" novalidate>
			<div style="margin-top:20px;margin-left:20px;" width="100%" cellspacing="0" cellpadding="0">
			    <div class="fitem">
                	<label>登录账号</label>
		        	<input class="easyui-validatebox" type="text"  name="login_name" readonly="true" ></input>
           		</div>
           		<div class="fitem">
                	<label>真实姓名</label>
		        	<input class="easyui-validatebox" type="text" name="real_name" readonly="true"></input>
           		</div>
           		<div class="fitem">
                	<label>电子邮箱</label>
		        	<input class="easyui-validatebox" type="text" name="email" readonly="true"></input>
           		</div>
           		<div class="fitem">
                	<label>用户状态</label>
		        	<input class="easyui-validatebox" type="text" id="viewUser_stats" readonly="true"></input>
           		</div>
           		<div class="fitem">
                	<label>绑定角色</label>
		        	<input class="easyui-validatebox" type="text" id="viewUser_bindedRoles" readonly="true"></input>
           		</div>
           		<div class="fitem">
                	<label>备注</label>
		        	<textarea style="height:70px;width:180px;" name="remarks" readonly="true"></textarea>
           		</div>
			</div>
			<div style="padding-left:260px;padding-top:10px;">
				<a href="javascript:void(0);" class="easyui-linkbutton" onClick="javascript:$('#w_viewUser').window('close');" iconCls="icon-undo" >返回</a>
			</div>
			</form>
	    </div>
	    <%-- 修改用户window --%>
	    <div id="w_editUser"  class="easyui-window"  data-options="title:'修改用户',maximizable:false,closed:true,minimizable:false,collapsible:false,width:550,modal:true,resizable:false">
			<form id="f_editUser" method="post" >
			<div style="margin-top:20px;margin-left:20px;" width="100%" cellspacing="0" cellpadding="0">
			    <div class="fitem">
                	<label>登录账号</label>
		        	<input class="easyui-validatebox" type="text" id="edit_login_name" name="login_name" readonly="true"></input>
           		</div>
           		<div class="fitem">
                	<label>真实姓名</label>
		        	<input class="easyui-validatebox" type="text" id="edit_real_name" name="real_name" data-options="required:true"></input>
           		</div>
           		<div class="fitem">
                	<label>电子邮箱</label>
		        	<input class="easyui-validatebox" type="text" id="edit_email" name="email" data-options="required:true"></input>
           		</div>
           		<div class="fitem">
                	<label>绑定角色</label>
		        	<input class="easyui-validatebox" type="text" id="userForRoleSel_edit"  data-options="required:true" readonly="true"></input>
      				<a id="menuBtnForRole_edit" onclick="javascript:showMenu_edit();" href="javascript:void(0);">&nbsp;选择</a>
      				<div id="menuContentForRole_edit" style=" border:1px solid #ccc;width:198px;height:176px;overflow:scroll;scroll-y;display:none;position: absolute; background-color:#fcfcfc;">
						<ul id="treeForRole_edit" class="ztree"	style="margin-top: 0; width: 140px;"></ul>
  					</div>
  					<input type="hidden" id="selectRole_edit" />
           		</div>
           		<div class="fitem">
                	<label>备注</label>
		        	<textarea style="height:70px;width:180px;" name="remarks" id="edit_remarks"></textarea>
           		</div>
			</div>
			<div style="padding-left:200px;padding-top:10px;">
				<a href="javascript:void(0);" id="editUser_btnOk" class="easyui-linkbutton" iconCls="icon-ok" >保存</a>
				<a href="javascript:void(0);" id="editUser_btnCancel" class="easyui-linkbutton" iconCls="icon-cancel" >取消</a>
			</div>
			</form>
	    </div>
	    
		</div>
</body>
</html>